load("@fbcode_macros//build_defs:cpp_library.bzl", "cpp_library")
load("@fbsource//tools/build_defs:rust_binary.bzl", "rust_binary")
load("@fbsource//tools/build_defs:rust_library.bzl", "rust_library")

oncall("sapling")

rust_library(
    name = "procinfo",
    srcs = glob(["src/**/*.rs"]),
    autocargo = {"cargo_toml_config": {
        "dependencies_override": {"target": {"'cfg(windows)'": {"dependencies": {"winapi": {
            "features": [
                "handleapi",
                "minwindef",
                "processthreadsapi",
                "psapi",
                "tlhelp32",
            ],
            "version": "0.3",
        }}}}},
        "extra_buck_dependencies": {"target": {'\'cfg(target_os = "macos")\'': {"build-dependencies": ["fbsource//third-party/rust:cc"]}}},
        "lib": {"name": "procinfo"},
        "package": {
            "authors": ["Meta Source Control Team <sourcecontrol-dev@meta.com>"],
            "homepage": "https://sapling-scm.com/",
            "license": "MIT",
            "name": "sapling-procinfo",
            "repository": "https://github.com/facebook/sapling",
        },
    }},
    crate_root = "src/lib.rs",
    os_deps = [
        (
            "macos",
            [
                ":macos_compat",
            ],
        ),
        (
            "windows",
            [
                "fbsource//third-party/rust:ntapi",
                "fbsource//third-party/rust:winapi",
            ],
        ),
    ],
    test_deps = select({
        "DEFAULT": [],
        "ovr_config//os:macos": [
            "fbsource//third-party/rust:tempfile",
        ],
    }),
    deps = [
        "fbsource//third-party/rust:libc",
    ],
)

cpp_library(
    name = "macos_compat",
    srcs = ["darwin.c"],
)

rust_binary(
    name = "ppid",
    srcs = ["examples/ppid.rs"],
    autocargo = {"ignore_rule": True},
    unittests = False,
    deps = [
        ":procinfo",
    ],
)

rust_binary(
    name = "rss",
    srcs = ["examples/rss.rs"],
    autocargo = {"ignore_rule": True},
    unittests = False,
    deps = [
        ":procinfo",
    ],
)

rust_binary(
    name = "lsof",
    srcs = ["examples/lsof.rs"],
    autocargo = {"ignore_rule": True},
    compatible_with = [
        "ovr_config//os:macos",
    ],
    unittests = False,
    deps = [
        ":procinfo",
    ],
)
